using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;

public class server_cs : MonoBehaviour {

	// Use this for initialization
    //CSdListener_TCP listener = new CSdListener_TCP();
    //CMyServer listener = new CMyServer();
    CSdPeer_UDP listener = new CSdPeer_UDP();
    public string lable_desc = "";
	void Start () {
	}
    void Awake()
    {
        CMyProtocolMgr pm = new CMyProtocolMgr();
        print("here is server start" + i++);
		listener.StartListen("", 9999, pm);
        //listener.StartListen("10.240.70.59", 9999, pm);
		//listener.StartListen("192.168.43.248", 9999, pm);
    }

    static int i = 1;
	// Update is called once per frame
	void Update () {
        //print("here is server " + i++);
        ServerRun();
	}
    //run server
    static bool m_bIsServerRunning = false;
    //clientrun
    void ServerRun()
    {
        //send
        if (m_bIsServerRunning == false)
        {
            m_bIsServerRunning = true;

        }
        listener.Run();
        //print("run the server");
    }
    //test protocol
    void TestProtocol()
    {
        string HostName = Dns.GetHostName();
        lable_desc = "hostname : " + Dns.GetHostName() + "\n";
        IPHostEntry MyEntry = Dns.GetHostByName(Dns.GetHostName());
        for (int i = 0; i < MyEntry.AddressList.Length; ++i )
        {
            IPAddress MyAddress = new IPAddress(MyEntry.AddressList[i].Address);
            lable_desc += MyAddress.ToString() + "\n";
        }
    }
    //ongui
    void OnGUI()
    {
        GUI.BeginGroup(new Rect(0, 0, Screen.width/2, 300f));
        // All rectangles are now adjusted to the group. (0,0) is the topleft corner of the group.

        // We'll make a box so you can see where the group is on-screen.
        GUI.Box(new Rect(0, 0, 300, 300), "server");
        GUI.Label(new Rect(0, 90, 300, 300), lable_desc);
        if (GUI.Button(new Rect(10.0f, 40.0f, 80.0f, 30.0f), "Start"))
        {
            TestProtocol();
        }

        // End the group we started above. This is very important to remember!
        GUI.EndGroup();
    }
}
